<?php

namespace addons\venue\controller;

use app\admin\model\venue\activity\UserActivity;
use app\common\controller\Api;
use \app\admin\model\venue\activity\Activity as Av;
use app\common\model\venue\activity\Cc;
use think\Request;

class Activity extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['list','detail','time'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    protected $config = "";

    public function __construct(Request $request = null)
    {
        $this->config = get_addon_config("venue");
        parent::__construct($request);
    }

    /*
     * 活动时间
     */
    public function time(){
        //获取今天开始后面7天的日期 需要返回星期几 几月几日
        $start_time = strtotime(date('Y-m-d'));
        $end_time = strtotime("+7 day", $start_time);
        $week = array("周日","周一","周二","周三","周四","周五","周六");
        $data = [];
        for ($i = $start_time;$i<$end_time;$i+=86400){
            $week_num = date("w",$i);
            $data[] = array("date"=>date("m.d",$i),"week_num"=>$week[$week_num],"str"=>strtotime(date("Y-m-d",$i)));
        }
        $this->success('返回成功',$data);

    }

    /*
     * 活动列表
     */
    public function list(){
        $lon = $this->request->header("lon")??'113.663221';
        $lat = $this->request->header("lat")??'34.7568711';
        $type   = $this->request->post('type');
        $page   = $this->request->post('page')??1;  //页码
        $limit  = $this->request->post('limit')??10; //每页显示条数
        $hd_time  = $this->request->post('hd_time')??""; //活动时间
        $where = '';
        if ($type){
            $where = array('venue_id'=>$type);
        }
        if ($hd_time){
            $hd_time = date("Y-m-d H:i:s",$hd_time);
            $data = Av::where('activity.state',"=","1")
                ->with(['venue'])
                ->whereRaw("'$hd_time' BETWEEN SUBSTRING_INDEX(workrange, ' - ', 1) AND SUBSTRING_INDEX(workrange, ' - ', -1)")
                ->where($where)
                ->field("venue.name,venue.address,".venue_getDistanceBuilder($lat, $lon))
                ->paginate(["page"=>$page,"list_rows"=>$limit]);
        }else{
            $data = Av::where('activity.state',"=","1")
                ->with(['venue'])
                ->where($where)
                ->field("venue.name,venue.address,".venue_getDistanceBuilder($lat, $lon))
                ->paginate(["page"=>$page,"list_rows"=>$limit]);
        }

        foreach ($data as $row) {
            $row->content_image = cdnurl($row->content_image,true);
            $row->image = cdnurl($row->image,true);
            $row->getRelation('venue')->visible(['name']);
            $row->hidden(['createtime',"updatetime","deletetime","weigh","state","venue"]);
        }

        $result = array("total" => $data->total(), "rows" => $data->items());
        $this->success('返回成功',$result);
    }

    /*
     * 活动详情
     */
    public function detail(){
        $id   = $this->request->post('id');
        if (!$id){
            $this->error("缺少活动id");
        }
        $data = Av::where('activity.state',"=","1")
            ->with(['venue'])
            ->where('activity.id',$id)
            ->field("venue.name,venue.lon,venue.lat,venue.address")
            ->find();
        $arr = [];
        for ($i = 1;$i<=$data['enroll_num'];$i++){
            $arr[$i-1]['id'] = $i;
            $arr[$i-1]['name'] = $i;
            $arr[$i-1]['disabled'] = false;
        }
        $data->enroll_num = $arr;
        $data->content_image = cdnurl($data->content_image,true);
        $data->image = cdnurl($data->image,true);
        $data->getRelation('venue')->visible(['name']);
        $data->hidden(['createtime',"updatetime","deletetime","weigh","state","venue"]);
        //活动场次
        $data->cc = Cc::where('activity_id',$id)->select();
        //计算可报名数量
        $data->enroll_num_left = Cc::where('activity_id',$id)->sum('enroll_num');
        $data->y_enroll_num = Cc::where('activity_id',$id)->sum('y_enroll_num');
        //报名情况
        $data->user_activity = UserActivity::where('activity_id',$id)->limit(3)->select()
                                ->each(function ($item){
                                    $user = \app\admin\model\User::get($item->user_id);
                                    if ($user){
                                        $item->username = $user['username'];
                                        $item->avatar = cdnurl($user['avatar'],true);
                                    }else{
                                        $item->username = '';
                                        $item->avatar = cdnurl('/assets/img/avatar.png',true);
                                    }
                                });
        $this->success('返回成功',$data);
    }

    /*
     * 我的活动列表
     */
    public function myList(){
        $userId = $this->auth->id;
        $data = UserActivity::with(["activity"])
                ->where("user_id",$userId)
                ->field("activity.id,activity.title,activity.type,activity.image")
                ->select();
        foreach ($data as $row) {
            $row->hidden(['activity',"createtime","user_id","activity_id"]);
        }
        $this->success('返回成功',$data);
    }


}
